import axios from "axios";
import HOST from "./HOST";
// import { Message, MessageBox } from "element-ui";
import { UserModule } from "@/store/modules/user";
import { getAppInstance } from "@/main";
const vue =getAppInstance()
const service = axios.create({
  baseURL: HOST,
  timeout: 5000
});

service.interceptors.request.use(
  config => {
    // if (UserModule.token) {
    //   config.headers["X-Access-Token"] = UserModule.token;
    // }
    console.log("vue",vue)
    return config;
  },
  error => {
    Promise.reject(error);
  }
);
service.interceptors.response.use(
  response => {
    const res = response.data;
    if (res.code !== 20000) {
      // Message({
      //   message: res.message || "Error",
      //   type: "error",
      //   duration: 5 * 1000
      // });
      if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
        // MessageBox.confirm(
        //   "You have been logged out, try to login again.",
        //   "Log out",
        //   {
        //     confirmButtonText: "Relogin",
        //     cancelButtonText: "Cancel",
        //     type: "warning"
        //   }
        // ).then(() => {
        //   UserModule.ResetToken();
        //   location.reload();
        // });
      }
      return Promise.reject(new Error(res.message || "Error"));
    } else {
      return response.data;
    }
  },
  error => {
    // Message({
    //   message: error.message,
    //   type: "error",
    //   duration: 5 * 1000
    // });
    return Promise.reject(error);
  }
);

export default service;
